<template>
	<view>
		<view class="item" @tap="aaa()">
			<text class="iconfont icon-shezhi"></text>
			服务地址
		</view>
		<view class="item" @tap="updataDz()">
			<text class="iconfont icon-shezhi"></text>
			更新地址
		</view>
		<view class="item" @tap="printSet()">
			<text class="iconfont icon-shezhi"></text>
			蓝牙设置
		</view>
		<view class="item" @tap="update()">
			<text class="iconfont icon-shezhi"></text>
			检查更新
		</view>
		<!-- 服务地址 -->
		<uni-popup ref="popup" type="dialog">
			<uni-popup-dialog mode="input" type="center" title="密码校验" placeholder='' :duration="2000"
				:before-close="true" @close="close" @confirm="confirm"></uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="popup1" type="dialog">
			<uni-popup-dialog mode="input" type="center" title="网络地址" placeholder='' :value="url" :duration="2000"
				:before-close="true" @close="close1" @confirm="confirm1"></uni-popup-dialog>
		</uni-popup>
		<!-- 更新地址 -->
		<uni-popup ref="popup2" type="dialog">
			<uni-popup-dialog mode="input" type="center" title="密码校验" placeholder='' :duration="2000"
				:before-close="true" @close="close2" @confirm="confirm2"></uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="popup3" type="dialog">
			<uni-popup-dialog mode="input" type="center" title="更新地址" placeholder='' :value="dz" :duration="2000"
				:before-close="true" @close="close3" @confirm="confirm3"></uni-popup-dialog>
		</uni-popup>
		<scanCode></scanCode>
	</view>
</template>

<script>
	import scanCode from '@/static/scan-code/scan-code.vue';
	export default {
		components: {
			scanCode
		},
		data() {
			return {
				url: '',
				dz: '',
				flag:false,
				isUrl:false,
				isDz:false
			}
		},
		onLoad() {
			this.url = uni.getStorageSync('url') == '' ? this.websiteUrl : uni.getStorageSync('url')
			this.dz = uni.getStorageSync('dz') == '' ? this.upDateDz : uni.getStorageSync('dz')
			let _this = this
			uni.$off('scancodedate')
			uni.$on('scancodedate', function(content) {
				if(!!_this.flag && !!_this.isUrl) {
					uni.setStorageSync('url',content)
					uni.showToast({
						title:'服务地址更新成功!',
						icon:'none',
						duration:3000,
					})
					uni.reLaunch({
						url: '../login/login'
					})
				} else if(!!_this.flag && !!_this.isDz) {
					uni.setStorageSync('dz',content)
					uni.showToast({
						title:'更新地址更新成功!',
						icon:'none',
						duration:3000,
					})
					uni.reLaunch({
						url: '../login/login'
					})
				}
				
			})
		},
		methods: {
			printSet() {
				uni.navigateTo({
					url:'../printSet/printSet'
				})
			},
			aaa() {
				this.$refs.popup.open('center')
			},
			updataDz() {
				this.$refs.popup2.open('center')
			},
			close() {
				this.$refs.popup.close()
			},
			toJSON() {},
			confirm(e) {
				if (e != '888888') {
					uni.showToast({
						title: '密码错误!',
						icon: 'none',
						duration: 3000
					})
					return
				}
				this.$refs.popup.close()
				this.$refs.popup1.open('center')
				this.flag = true
				this.isUrl = true
				this.isDz = false
			},
			close1() {
				this.$refs.popup1.close()
			},
			confirm1(e) {
				uni.setStorageSync('url', e)
				console.log('url', e)
				this.$refs.popup1.close()
				this.flag = false
			},
			close2() {
				this.$refs.popup2.close()
			},
			close3() {
				this.$refs.popup3.close()
			},
			confirm3(e) {
				uni.setStorageSync('dz', e)
				this.$refs.popup3.close()
				this.flag = false
			},
			confirm2(e) {
				if (e != '888888') {
					uni.showToast({
						title: '密码错误!',
						icon: 'none',
						duration: 3000
					})
					return
				}
				this.$refs.popup2.close()
				this.$refs.popup3.open('center')
				this.flag = true
				this.isDz = true
				this.isUrl = false
			},
			update() {
				uni.request({
					url: `http://${this.dz}/Update.txt`,
					method: 'GET',
					dataType: 'json',
					success: (r) => {
						let Version = uni.getStorageSync('bbh') == '' ? this.Version : uni.getStorageSync(
							'bbh')
						//console.log(this.Version,'this.Version')
						uni.showModal({
							content: '点击确定后不要退出,程序将启动自动更新。新版本下载完成后将自动弹出安装程序。',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									//console.log('用户点击确定');
									dtask.start();
								}
							}
						});
						var dtask = plus.downloader.createDownload(r.data.url, {
							force: true
						}, function(d,
							status) {
							// 下载完成    
							if (status == 200) {
								plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {},
									function(fileEntry) {
										//this.Version = r.data.Version
										uni.setStorageSync('bbh', r.data.Version)
									},
									function(error) {
										uni.showToast({
											title: '安装失败',
											mask: false,
											duration: 1500
										});
									})
							} else {
								uni.showToast({
									title: '更新失败',
									mask: false,
									duration: 1500
								});
							}
						});


					}
				})
			}
		}
	}
</script>

<style>
	.item {
		height: 100upx;
		padding: 0 10upx;
		border-bottom: 1px solid #CCCCCC;
		display: flex;
		align-items: center;
	}

	.iconfont {
		font-size: 60upx;
		display: inline-block;
		margin-right: 10upx;
	}
</style>
